# File: hw3_3.py
# This program translates a DNA string into its protein sequence
# in frames (+1, +2, +3)

# Define standard codon table
standard = {'ttt': 'F', 'tct': 'S', 'tat': 'Y', 'tgt': 'C',
            'ttc': 'F', 'tcc': 'S', 'tac': 'Y', 'tgc': 'C',
            'tta': 'L', 'tca': 'S', 'taa': '*', 'tga': '*',
            'ttg': 'L', 'tcg': 'S', 'tag': '*', 'tgg': 'W',

            'ctt': 'L', 'cct': 'P', 'cat': 'H', 'cgt': 'R',
            'ctc': 'L', 'ccc': 'P', 'cac': 'H', 'cgc': 'R',
            'cta': 'L', 'cca': 'P', 'caa': 'Q', 'cga': 'R',
            'ctg': 'L', 'ccg': 'P', 'cag': 'Q', 'cgg': 'R',

            'att': 'I', 'act': 'T', 'aat': 'N', 'agt': 'S',
            'atc': 'I', 'acc': 'T', 'aac': 'N', 'agc': 'S',
            'ata': 'I', 'aca': 'T', 'aaa': 'K', 'aga': 'R',
            'atg': 'M', 'acg': 'T', 'aag': 'K', 'agg': 'R',

            'gtt': 'V', 'gct': 'A', 'gat': 'D', 'ggt': 'G',
            'gtc': 'V', 'gcc': 'A', 'gac': 'D', 'ggc': 'G',
            'gta': 'V', 'gca': 'A', 'gaa': 'E', 'gga': 'G',
            'gtg': 'V', 'gcg': 'A', 'gag': 'E', 'ggg': 'G'}

print 'Enter path to file containing DNA string:'

# Ask for path to file containing DNA string
# and load DNA string into memory
dna = open(raw_input(), 'U').read()

print "Enter name of output string:"

# Ask under what name to store output string
# and create output file
output = open(raw_input(), 'w')

# Determine length of DNA string
dna_size = len(dna)

# Ask for offset
print "Enter desired frame (+1,+2,+3):"

# Initialize counters
counter = (int(raw_input())-1) % 3
triple = 0
amino_acid = ''

# Go through the entire DNA string
while counter < dna_size:
    # Everytime a base pair is read, add it to 'amino_acid'
    if dna[counter] == 'g' or dna[counter] == 'G' \
        or dna[counter] == 'c' or dna[counter] == 'C' \
        or dna[counter] == 'a' or dna[counter] == 'A' \
        or dna[counter] == 't' or dna[counter] == 'T':
            # If 'amino_acid' does not yet contain three base pairs,
            # then add one more
            if triple < 3:
                amino_acid += dna[counter]
                triple += 1
                # If it does, then translate the triple into
                # the corresponding amino acid and output to file
                if triple == 3:
                    output.write(standard[amino_acid])
                    triple = 10
            # Reset counters after having written an amino acid to file
            elif triple == 10:
                triple = 1
                amino_acid = dna[counter]
    # Maintain layout
    elif dna[counter] == '\n':
        output.write(dna[counter])
    counter += 1
